package com.cropflow.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.http.ResponseEntity;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Arrays;

@Controller
@RequestMapping("/community")
public class CommunityController {
    
    // 农友圈页面
    @GetMapping("/circle")
    public String circlePage(Model model) {
        // 模拟数据，实际应从数据库获取
        model.addAttribute("posts", getMockPosts());
        model.addAttribute("hotTopics", getMockHotTopics());
        model.addAttribute("recommendedUsers", getMockRecommendedUsers());
        return "community/circle";
    }
    
    // 交流区页面
    @GetMapping("/forum")
    public String forumPage(Model model) {
        model.addAttribute("categories", getMockCategories());
        model.addAttribute("discussions", getMockDiscussions());
        return "community/forum";
    }
    
    // 发布动态API
    @PostMapping("/api/posts")
    @ResponseBody
    public ResponseEntity<?> createPost(@RequestBody Map<String, Object> postData) {
        // 处理发布逻辑
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "发布成功");
        return ResponseEntity.ok(result);
    }
    
    // 点赞API
    @PostMapping("/api/posts/{id}/like")
    @ResponseBody
    public ResponseEntity<?> likePost(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("likes", 42);
        return ResponseEntity.ok(result);
    }
    
    // 评论API
    @PostMapping("/api/posts/{id}/comments")
    @ResponseBody
    public ResponseEntity<?> addComment(@PathVariable Long id, @RequestBody Map<String, String> comment) {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "评论成功");
        return ResponseEntity.ok(result);
    }
    
    // 模拟数据方法
    private List<Map<String, Object>> getMockPosts() {
        List<Map<String, Object>> posts = new ArrayList<>();
        Map<String, Object> post1 = new HashMap<>();
        post1.put("id", 1L);
        post1.put("author", "张农夫");
        post1.put("content", "今天的玉米长势不错，感谢大家的建议！");
        post1.put("likes", 15);
        post1.put("comments", 3);
        posts.add(post1);
        return posts;
    }
    
    private List<String> getMockHotTopics() {
        return Arrays.asList("#春耕技巧", "#病虫害防治", "#智能农机");
    }
    
    private List<Map<String, String>> getMockRecommendedUsers() {
        List<Map<String, String>> users = new ArrayList<>();
        Map<String, String> user1 = new HashMap<>();
        user1.put("name", "李专家");
        user1.put("title", "农业技术专家");
        users.add(user1);
        return users;
    }
    
    private List<String> getMockCategories() {
        return Arrays.asList("技术交流", "设备讨论", "经验分享", "问题求助");
    }
    
    private List<Map<String, Object>> getMockDiscussions() {
        return new ArrayList<>();
    }
}